package com.bqtv.sport.commen.utils

import android.content.Context
import android.content.res.AssetManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.text.TextUtils


class SavePicUtils {
    companion object {
        fun readBitmapFromAssets(ctx: Context, fileName: String): Bitmap? {
            val assetManager: AssetManager = ctx.assets
            var bitmap: Bitmap? = null
            try {
                val inputStream = assetManager.open(fileName)
                bitmap = BitmapFactory.decodeStream(inputStream)
                inputStream.close()
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return bitmap
        }

        fun getMoneyBitmap(
            backgroundBitmap: Bitmap,
            qrCodeBitmap: Bitmap,
            name: String=""
        ): Bitmap {
            var width = backgroundBitmap.width
            var height = backgroundBitmap.height
            val result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(result)
            canvas.drawBitmap(backgroundBitmap, 0f, 0f, null)
            // 绘制二维码
            val qrCodeLeft: Int = (backgroundBitmap.getWidth() - qrCodeBitmap.getWidth()) / 2
            val qrCodeTop: Int = (backgroundBitmap.getHeight() - qrCodeBitmap.getHeight()) * 2 / 5
            canvas.drawBitmap(qrCodeBitmap, qrCodeLeft.toFloat(), qrCodeTop.toFloat(), null)
            // 绘制文字
            val paint = Paint()
            paint.color = Color.BLACK // 设置文字颜色

            paint.textSize = 35f // 设置文字大小

            paint.textAlign = Paint.Align.CENTER // 设置文字对齐方式

            canvas.drawText(
                name,
                backgroundBitmap.getWidth() / 2f,
                qrCodeTop + qrCodeBitmap.getHeight() + 80f,
                paint
            ) // 在指定位置绘制文字
            return result
        }

        fun getUserBitmap(
            backgroundBitmap: Bitmap,
            qrCodeBitmap: Bitmap,
            name: String?
        ): Bitmap {
            var width = backgroundBitmap.width
            var height = backgroundBitmap.height
            val result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(result)
            canvas.drawBitmap(backgroundBitmap, 0f, 0f, null)
            // 绘制二维码
            val qrCodeLeft: Int = (backgroundBitmap.getWidth() - qrCodeBitmap.getWidth()) / 2
            val qrCodeTop: Int = (backgroundBitmap.getHeight() - qrCodeBitmap.getHeight()) / 2
            canvas.drawBitmap(qrCodeBitmap, qrCodeLeft.toFloat(), qrCodeTop.toFloat(), null)
            // 绘制文字
            val paint = Paint()
            paint.color = Color.BLACK // 设置文字颜色

            paint.textSize = 50f // 设置文字大小

            paint.textAlign = Paint.Align.CENTER // 设置文字对齐方式

            name?.let {
                canvas.drawText(
                    it,
                    backgroundBitmap.getWidth() / 2f,
                    backgroundBitmap.getHeight() - 100f,
                    paint
                )
            } // 在指定位置绘制文字
            return result
        }
    }
}